package org.studiox.graph.runtime.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.studiox.graph.common.GraphLineage;
import org.studiox.graph.runtime.service.GraphService;

@RestController
@RequestMapping(value = "/studiox/api/graph/runtime/v1/lineage")
@Tag(name = "Lineage API Interface", description = "Lineage API 接口")
public class LineageController {

  @Autowired private GraphService graphRuntimeService;

  @Operation(
      summary = "查询血缘版本信息",
      description = "根据指定条件查询并返回血缘版本信息",
      parameters = {
        @Parameter(
            name = "version",
            schema = @Schema(implementation = String.class),
            description = "版本信息")
      },
      responses = {
        @ApiResponse(
            description = "返回查询结果",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = String.class))),
        @ApiResponse(responseCode = "400", description = "返回 400 Error")
      })
  @RequestMapping(value = "/version", method = RequestMethod.GET)
  public String getVersion(@RequestParam(value = "version", required = false) String version) {
    return "Graph Runtime Server Version: " + version;
  }

  @RequestMapping(value = "/lineageInfo", method = RequestMethod.GET)
  public GraphLineage getLineageInfo(
      @RequestParam(value = "unique", required = true) String unique,
      @RequestParam(value = "depth", required = false, defaultValue = "-1") Integer depth,
      @RequestParam(value = "skipDeleted", required = false, defaultValue = "true")
          Boolean skipDeleted) {
    return graphRuntimeService.getLineage(unique, depth, skipDeleted);
  }

  @RequestMapping(value = "/impactInfo", method = RequestMethod.GET)
  public GraphLineage getImpactInfo(
      @RequestParam(value = "unique", required = true) String unique,
      @RequestParam(value = "depth", required = false, defaultValue = "-1") Integer depth,
      @RequestParam(value = "skipDeleted", required = false, defaultValue = "true")
          Boolean skipDeleted) {
    return graphRuntimeService.getImpact(unique, depth, skipDeleted);
  }

  @RequestMapping(value = "/fullLineageInfo", method = RequestMethod.GET)
  public GraphLineage getFullLineageInfo(
      @RequestParam(value = "unique", required = true) String unique,
      @RequestParam(value = "depth", required = false, defaultValue = "-1") Integer depth,
      @RequestParam(value = "skipDeleted", required = false, defaultValue = "true")
          Boolean skipDeleted) {
    return graphRuntimeService.getFullLineage(unique, depth, skipDeleted);
  }
}
